package cn.openread.demo.video;

import java.util.Comparator;

/**
 * 用途：一切为了业务
 * <p>
 * Created by simon on 2020-02-02.
 */
public class StringComparator implements Comparator<String> {
    @Override
    public int compare(String s1, String s2) {
        if (returnDouble(s1) < returnDouble(s2))
            return -1;
        else if (returnDouble(s1) > returnDouble(s2))
            return 1;
        else
            return 0;
    }

    public static double returnDouble(String str) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i)))
                sb.append(str.charAt(i));
            else if (str.charAt(i) == '.' && i < str.length() - 1 && Character.isDigit(str.charAt(i + 1)))
                sb.append(str.charAt(i));
            else break;
        }
        if (sb.toString().isEmpty())
            return 0;
        else
            return Double.parseDouble(sb.toString());
    }
}
